package dtu.app.fragment;

import java.util.List;

import main.java.com.fortysevendeg.android.swipelistview.SwipeListView;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.iqengines.demo.R;

import dtu.app.listadapter.ProductArrayAdapter;
import dtu.app.listloader.ProductListLoader;
import dtu.app.model.Product;

public class ProductListFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Product>> {
	
	public interface UpdateCallback {
		public void update(String category);
	}
	
	String mCurrentCategory;
	UpdateCallback mUpdateCallback;
	ProductArrayAdapter mAdapter;
	SwipeListView mListView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v = inflater.inflate(R.layout.products, null);
		mListView = (SwipeListView) v.findViewById(android.R.id.list);
		return v;
	}
	
	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		try {
			mUpdateCallback = (UpdateCallback) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
	}
	
	@Override 
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		System.out.println("DataListFragment.onActivityCreated");

		// Initially there is no data 
		//mListView.setEmptyText("No Data Here");

		// Create an empty adapter we will use to display the loaded data.
		mAdapter = new ProductArrayAdapter(getActivity(), this);
		mListView.setAdapter(mAdapter);

		// Start out with a progress indicator.
		mListView.setVisibility(View.INVISIBLE);
		//setListShown(false);

		// Prepare the loader.  Either re-connect with an existing one,
		// or start a new one.
		getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public Loader<List<Product>> onCreateLoader(int arg0, Bundle bundle) {
		Log.i("DataListFragment", "DataListFragment.onCreateLoader");
		return new ProductListLoader(getActivity(), bundle);
	}

	@Override
	public void onLoadFinished(Loader<List<Product>> arg0, List<Product> data) {
		mAdapter.setData(data);
		Log.i("DataListFragment", "DataListFragment.onLoadFinished");

		// The list should now be shown.
		if (isResumed()) {
			mListView.setVisibility(View.VISIBLE);
		} else {
			mListView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onLoaderReset(Loader<List<Product>> arg0) {
		mAdapter.setData(null);
	}

	public void updateList(String category) {	
		Log.i("DataListFragment", "updateList");
		Bundle b = new Bundle();
		b.putString("category", category);
		getLoaderManager().restartLoader(0, b, this);
		mUpdateCallback.update(mCurrentCategory);
	}
}